library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 10.1
summary(factor(df$Q21)) # Iran
summary(factor(df$Q25)) # Transgender ban
summary(factor(df$Q11)) 

df2 <- df
df2 <- df2[df2$Q11 < 77,]

# Attack Iran treatments
df2$P_ASSIGN2A <- NA
df2$P_ASSIGN2A[df2$P_ASSIGN2==1] <- 1 # Control
df2$P_ASSIGN2A[df2$P_ASSIGN2==5] <- 2 # Oppose
df2$P_ASSIGN2A[df2$P_ASSIGN2==4] <- 3 # Support

# Transgender Ban treatments
# P_ASSIGN3 == 1    # Control
# P_ASSIGN3 == 2    # Oppose
# P_ASSIGN3 == 3    # Support
# P_ASSIGN3 == 4    # Divided

# Q21T
df2$Q21T <- NA
df2$Q21T[df2$Q21 < 3] <- 1
df2$Q21T[df2$Q21 > 2 & df2$Q21 < 77] <- 0

# Q25T
df2$Q25T <- NA
df2$Q25T[df2$Q25 < 3] <- 1
df2$Q25T[df2$Q25 > 2 & df2$Q25 < 77] <- 0

# Create binaries for conditions
df2$isupp <- NA
df2$isupp[df2$P_ASSIGN2A==1] <- 0
df2$isupp[df2$P_ASSIGN2A==3] <- 1

df2$iopp <- NA
df2$iopp[df2$P_ASSIGN2A==1] <- 0
df2$iopp[df2$P_ASSIGN2A==2] <- 1

df2$tbo <- NA
df2$tbo[df2$P_ASSIGN3==1] <- 0
df2$tbo[df2$P_ASSIGN3==2] <- 1

df2$tbs <- NA
df2$tbs[df2$P_ASSIGN3==1] <- 0
df2$tbs[df2$P_ASSIGN3==3] <- 1

df2$tbd <- NA
df2$tbd[df2$P_ASSIGN3==1] <- 0
df2$tbd[df2$P_ASSIGN3==4] <- 1


# Survey designs
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

# T-tests
# Iran
iopp <- svyttest(Q21T~iopp, w2_design, na.rm = TRUE)
isup <- svyttest(Q21T~isupp, w2_design, na.rm = TRUE)

# Transgender ban
tbo <- svyttest(Q25T~tbo, w2_design, na.rm = TRUE)
tbs <- svyttest(Q25T~tbs, w2_design, na.rm = TRUE)
tbd <- svyttest(Q25T~tbd, w2_design, na.rm = TRUE)

# Build out plots
# Iran
pt.est <- c(isup$estimate, 
            iopp$estimate)
ci.low <- c(isup$conf.int[1], 
            iopp$conf.int[1])
ci.high <- c(isup$conf.int[2], 
             iopp$conf.int[2])
plot.labs <- c("Support","Oppose")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

g.i <- ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle("Iran Strikes")+
  theme_bw()

# TB
pt.est <- c(tbs$estimate, 
            tbo$estimate, 
            tbd$estimate)
ci.low <- c(tbs$conf.int[1], 
            tbo$conf.int[1],
            tbd$conf.int[1])
ci.high <- c(tbs$conf.int[2], 
             tbo$conf.int[2],
             tbd$conf.int[2])
plot.labs <- c("Support","Oppose", "Divided")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

g.tb <- ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle("Transgender Ban")+
  theme_bw()

grid.arrange(g.i,g.tb,ncol=2)
